//
//  SignRedTableViewCell.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/1.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class SignRedTableViewCell: UITableViewCell {
    fileprivate let SignRedCollectionViewCellID = "SignRedCollectionViewCell"
    fileprivate let SignRedCollectionReusableViewID = "SignRedCollectionReusableView"

    var lineImageView:UIImageView = {
        let imageView = UIImageView.init(frame: FRAME_CGFloat(31.5, 0, 0.5, 91.5))
        imageView.backgroundColor = RGB(219, 233, 255)
        
        return imageView
    }()
    
    var leftImageView:UIImageView = {
        let imageView = UIImageView.init(frame: FRAME_CGFloat(20, 0, 23, 23))
        imageView.image = IMAGE(name: "signRed_未进行")
        imageView.layer.cornerRadius = 11.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()

    var model = SignInListModel()
    var modelList: SignInListModel? {
        didSet {
            guard let model = modelList else { return }
            self.model = model
            self.collectionView.reloadData()
            
            if self.model.week == 4 {
                self.lineImageView.isHidden = true
            } else {
                self.lineImageView.isHidden = false
            }
        }
    }
    
    fileprivate lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize.init(width: 27, height: 50)
        layout.headerReferenceSize = CGSize.init(width: SCREEN_WIDTH - 90, height: 26)
//        layout.sectionInset = UIEdgeInsets.init(top: 6, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = (SCREEN_WIDTH - 279) / 6
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(SignRedCollectionViewCell.self, forCellWithReuseIdentifier: SignRedCollectionViewCellID)
        collectionView.register(SignRedCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: SignRedCollectionReusableViewID)
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUI()
    }
    
    
    func setUI() {
        self.addSubview(self.lineImageView)
        self.addSubview(self.leftImageView)
        
        self.collectionView.frame = FRAME_CGFloat(64.5, 0, SCREEN_WIDTH - 90, 74)
        self.collectionView.backgroundColor = WhiteBackColor
        self.addSubview(self.collectionView)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//  MARK: - ###### 协议方法 ######
extension SignRedTableViewCell:UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model.subSignInBo?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SignRedCollectionViewCellID, for: indexPath) as! SignRedCollectionViewCell
        cell.modelList = self.model.subSignInBo?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView : SignRedCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: SignRedCollectionReusableViewID, for: indexPath) as! SignRedCollectionReusableView
        headerView.setLeftLabel(week: self.model.week)
        headerView.rightLabel.text = setRightLabel(status: self.model.signStatus, count: self.model.countSignIn)
        return headerView
    }
    
}

//  MARK: - ###### 事件 ######
extension SignRedTableViewCell {
    func setRightLabel(status:Int,count:Int) -> String {
        switch status {
        case 0:
            self.leftImageView.image = IMAGE(name: "signRed_未进行")
            return "未解锁"
        case 1:
            self.leftImageView.image = IMAGE(name: "signRed_成功")
            return "已经签到\(count)天"
        case 2:
            self.leftImageView.image = IMAGE(name: "signRed_成功")
            return "正在签到\(count)天"
        case 3:
            self.leftImageView.image = IMAGE(name: "signRed_成功")
            return "待签到"
        default:
            self.leftImageView.image = IMAGE(name: "signRed_未进行")
            return ""
        }
    }
}
